﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Experimentor.CheckResponsiveness
{
    public partial class form_CheckResponsiveness : Form
    {
        private int currentstage;
        private CheckResponsivenessWorker workerthread;
        private RunWorkerCompletedEventHandler workercompletehandler;
        
        public form_CheckResponsiveness()
        {
            InitializeComponent();
            this.currentstage = CheckResponsivenessWorker.STAGE_BEST;
            this.workercompletehandler = new RunWorkerCompletedEventHandler(this.workerCompleted);
        }

        private void form_CheckResponsiveness_Load(object sender, EventArgs e)
        {
            this.workerthread = new CheckResponsivenessWorker(this.currentstage, this.workercompletehandler);
            this.workerthread.RunWorkerAsync();
        }

        private void workerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (((CheckResponsivenessWorker)sender).stage == CheckResponsivenessWorker.STAGE_BEST)
            {
                this.label_BestConditions.Text = ((Double)e.Result).ToString("F4") + " ms";
                this.workerthread = new CheckResponsivenessWorker(++this.currentstage, this.workercompletehandler);
                workerthread.RunWorkerAsync();
                return;
            } else if (((CheckResponsivenessWorker)sender).stage == CheckResponsivenessWorker.STAGE_MEDIUM)
            {
                this.label_MediumConditions.Text = ((Double)e.Result).ToString("F4") + " ms";
                this.workerthread = new CheckResponsivenessWorker(++this.currentstage, this.workercompletehandler);
                this.workerthread.RunWorkerAsync();
                return;
            }
            else if (((CheckResponsivenessWorker)sender).stage == CheckResponsivenessWorker.STAGE_HARD)
            {
                this.label_HardConditions.Text = ((Double)e.Result).ToString("F4") + " ms";
                this.currentstage++;
                this.button_Cancel.Enabled = false;
                return;
            }
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            if (this.workerthread.IsBusy)
            {
                this.workerthread.RunWorkerCompleted -= this.workercompletehandler;
                this.workerthread.CancelAsync();
                if (this.currentstage <= CheckResponsivenessWorker.STAGE_BEST) this.label_BestConditions.Text = "CANCELLED";
                if (this.currentstage <= CheckResponsivenessWorker.STAGE_MEDIUM) this.label_MediumConditions.Text = "CANCELLED";
                if (this.currentstage <= CheckResponsivenessWorker.STAGE_HARD) this.label_HardConditions.Text = "CANCELLED";
                this.button_Cancel.Enabled = false;
            }
        }

        private void form_CheckResponsiveness_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.workerthread.IsBusy) this.button_Cancel_Click(this, new EventArgs());
        }

    }
}
